package cn.iocoder.yudao.module.system.service.userReal;

import cn.iocoder.yudao.framework.common.util.encryption.EncryptionUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.userReal.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userReal.UserRealDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.system.convert.userReal.UserRealConvert;
import cn.iocoder.yudao.module.system.dal.mysql.userReal.UserRealMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户实名制 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class UserRealServiceImpl implements UserRealService {

    @Resource
    private UserRealMapper userRealMapper;

    @Override
    public Long createUserReal(UserRealCreateReqVO createReqVO) {
        // 插入
        UserRealDO userReal = UserRealConvert.INSTANCE.convert(createReqVO);
        userReal.setCardNo(EncryptionUtil.desDecrypt(userReal.getCardNo()));
        userRealMapper.insert(userReal);
        // 返回
        return userReal.getId();
    }

    @Override
    public Long createORupdateUserReal(@Valid UserRealCreateReqVO createReqVO) {
        UserRealDO userRealDO=userRealMapper.selectOne("card_no",EncryptionUtil.desDecrypt(createReqVO.getCardNo()));
        // 校验存在
        if(userRealDO == null){
            LoginUser loginUser=SecurityFrameworkUtils.getLoginUser();

            UserRealDO userReal = UserRealConvert.INSTANCE.convert(createReqVO);
            userReal.setCardNo(EncryptionUtil.desDecrypt(userReal.getCardNo()));
            userReal.setUserId(loginUser.getId());
            // 插入
            userRealMapper.insert(userReal);
            return userReal.getId();
        }else{
            userRealDO.setCardNo(EncryptionUtil.desDecrypt(userRealDO.getCardNo()));
            userRealDO.setDeleted(false);
            userRealMapper.updateById(userRealDO);
            return userRealDO.getId();
        }

        // 返回

    }

    @Override
    public void updateUserReal(UserRealUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserRealExists(updateReqVO.getId());
        // 更新
        UserRealDO updateObj = UserRealConvert.INSTANCE.convert(updateReqVO);
        updateObj.setCardNo(EncryptionUtil.desDecrypt(updateObj.getCardNo()));
        userRealMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserReal(Long id) {
        // 校验存在
        this.validateUserRealExists(id);
        // 删除
        userRealMapper.deleteById(id);
    }

    private void validateUserRealExists(Long id) {
        if (userRealMapper.selectById(id) == null) {
            throw exception(USER_REAL_NOT_EXISTS);
        }
    }

    @Override
    public UserRealDO getUserReal(Long id) {

        return userRealMapper.selectById(id);
    }

    @Override
    public List<UserRealDO> getUserRealList(Collection<Long> ids) {
        return userRealMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserRealDO> getUserRealPage(UserRealPageReqVO pageReqVO) {
        return userRealMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserRealDO> getUserRealList(UserRealExportReqVO exportReqVO) {
        return userRealMapper.selectList(exportReqVO);
    }

}
